package com.honey.mybatis.generator.dao.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.Method;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoConstant;

/**
 * count查询
 * @author Administrator
 *
 */
public class CountMethod extends AbstractCreateInterfaceMethod{

	private static final String METHOD_NAME="count";
	
	private static final FullyQualifiedJavaType RETURN_TYPE = JDKFullyQualifiedJavaType.getIntInstance();
	
	private boolean isUseSession = false ;
	
	public CountMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	public CountMethod(
			AbstractJavaCompilation clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn,
			boolean  isUseSession
			) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
		this.isUseSession = isUseSession;
	}
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {	
		IfBlock ifBlock = null;
		if( isUseSession()){
			ifBlock = new IfBlock(new StringBuilder().append( DaoConstant.SQL_SESSION_NAME ).append(" == null").toString());
			ifBlock.addBodyLine("return 0");
			method.addBlocks(ifBlock);
		}
		ifBlock = new IfBlock(new StringBuilder()
			.append(DaoConstant.CONDITION_VARIABLE_NAME)
			.append(" == null") 
			.append( " || "+ DaoConstant.CONDITION_VARIABLE_NAME +".getOredCriteria().size() == 0" )
			.toString()
		);
		ifBlock.addBodyLine(DaoConstant.CONDITION_VARIABLE_NAME+" = null");
		method.addBlocks(ifBlock);
		
		method.addBodyLine(
			new StringBuilder()
			.append("Integer count = ( Integer )")
			.append(isUseSession?DaoConstant.SQL_SESSION_NAME:super.getInversionModel().call( super.getDependentModel() , null))
			.append(".selectOne( ")
			//.append(DaoConstant.SELECT_COUNT_FIELD_NAME)
			.append(super.getMapperId().getSelectCountMapperId())
			.append(", ")
			.append(DaoConstant.CONDITION_VARIABLE_NAME).append(')')
			.toString()
		);
		method.addBodyLine("return count.intValue()");
	}

	@Override
	protected String getMethodName() {
		String answer = null;
		if( !isUseSession ){
			answer = METHOD_NAME;
		}else if( isUseSession  && mybatisPlugin.isEnableUseSqlSession() ){
			answer = METHOD_NAME;
		}
		return answer;
	}

	/**
	 * @return the isUseSession
	 */
	@Override
	public final boolean isUseSession() {
		return isUseSession;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		String[] answer = new String[]{
			"根据条件查询记录集数量。",
		};
		return answer;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		
		return RETURN_TYPE;
	}
}
